﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace QCYHApp
{
    class InitFile
    {
        public static string title = "七彩云和传输程序 V1.00";
        public static string url = ConfigurationManager.AppSettings["url"].ToString();
        public static string apiKey = ConfigurationManager.AppSettings["apiKey"].ToString();
        public static string Auto = ConfigurationManager.AppSettings["Auto"].ToString();
        //public static string apiSecret = ConfigurationManager.AppSettings["apiSecret"].ToString();
        public static string apiSecret = "8YasgucfcT#Ovk9viqfiljjuHUb@&cI^";
        

        #region 判断当前时间是否在工作时间段内
        public bool getTimeSpan(string _strWorkingDayAM, string _strWorkingDayPM)
        {
            //判断当前时间是否在工作时间段内
            //string _strWorkingDayAM = "01:00";
            //string _strWorkingDayPM = "11:00";
            TimeSpan dspWorkingDayAM = DateTime.Parse(_strWorkingDayAM).TimeOfDay;
            TimeSpan dspWorkingDayPM = DateTime.Parse(_strWorkingDayPM).TimeOfDay;
            //string time1 = "2017-2-17 8:10:00";
            //DateTime t1 = Convert.ToDateTime(timeStr);
            DateTime t1 = DateTime.Now;
            TimeSpan dspNow = t1.TimeOfDay;
            if (dspNow > dspWorkingDayAM && dspNow < dspWorkingDayPM)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 获取时间戳
        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <returns></returns>
        public static string GetTimeStamp()
        {
            TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds).ToString();
        }
        #endregion

        #region MD5加密
        public static string md5(string str)
        {
            try
            {
                MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                byte[] bytValue, bytHash;
                bytValue = System.Text.Encoding.UTF8.GetBytes(str);
                bytHash = md5.ComputeHash(bytValue);
                md5.Clear();
                string sTemp = "";
                for (int i = 0; i < bytHash.Length; i++)
                {
                    sTemp += bytHash[i].ToString("X").PadLeft(2, '0');
                }
                str = sTemp.ToLower();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return str;
        }
        #endregion

        #region Unicode转中文
        public string FromUnicodeString(string str, string SplitString = "u")
        {
            string regexCode = SplitString == "u" ? "\\\\u(\\w{1,4})" : SplitString + "(\\w{1,4})";
            string reString = str;
            System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(regexCode);
            System.Text.RegularExpressions.MatchCollection mc = reg.Matches(reString);
            for (int i = 0; i < mc.Count; i++)
            {
                try
                {
                    var outs = (char)int.Parse(mc[i].Groups[1].Value, System.Globalization.NumberStyles.HexNumber);
                    str = str.Replace(mc[i].Groups[0].Value, outs.ToString());
                }
                catch
                {
                    continue;
                }
            }
            return str;
        }
        #endregion

        #region 日志
        /// <summary>
        /// 生成日志文件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="isdebug"></param>
        public void WriteLog(string msg, bool isdebug, string title = "")
        {
            string text = AppDomain.CurrentDomain.BaseDirectory;
            text = Path.Combine(text, "log");
            if (!Directory.Exists(text))
            {
                Directory.CreateDirectory(text);
            }
            if (isdebug)
            {
                text = Path.Combine(text, "debug");
                if (!Directory.Exists(text))
                {
                    Directory.CreateDirectory(text);
                }
            }
            text = string.IsNullOrEmpty(title) ? Path.Combine(text, DateTime.Now.ToString("yyyyMMdd") + ".txt") : Path.Combine(text, DateTime.Now.ToString("yyyyMMdd") + title + ".txt");
            if (!File.Exists(text))
            {
                File.CreateText(text).Close();
            }
            StreamWriter streamWriter = new StreamWriter(text, true, Encoding.UTF8);
            if (isdebug)
            {
                streamWriter.WriteLine("--------------------------------------------------------------------------------------------------------------------------------");
            }
            streamWriter.WriteLine("{0}  {1}", DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), msg);
            if (isdebug)
            {
                streamWriter.WriteLine("--------------------------------------------------------------------------------------------------------------------------------");
            }
            streamWriter.Flush();
            streamWriter.Close();
        }
        /// <summary>
        /// 清理日志文件
        /// </summary>
        public void ClearLog()
        {
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log");
            if (Directory.Exists(path))
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(path);
                FileInfo[] files = directoryInfo.GetFiles("*.*", SearchOption.AllDirectories);
                FileInfo[] array = files;
                for (int i = 0; i < array.Length; i++)
                {
                    FileInfo fileInfo = array[i];
                    if (DateTime.Now.Subtract(fileInfo.CreationTime).Days > 30)
                    {
                        File.Delete(fileInfo.FullName);
                    }
                }
            }
        }
        #endregion        
    }
}
